window.onload = function () {
  var currentPage = 1;
  var pagesize;
  var totalCount;
  var pageCount;

  saveMoney(currentPage)

  function saveMoney(currentPage) {
    $.ajax({
      type: "get",
      url: "http://193.112.55.79:9090/api/getmoneyctrl",
      datatype: "json",
      data: {
        pageid: currentPage
      },
      async: false, //注意这里async，默认为true表示异步
      success: function (res) {
        // console.log(res);
        template.config("escape", false);
        var htmlStr = template("saveMoneyTmp", res);
        $(".goods_list").html(htmlStr);
        // 给全局变量赋值
        totalCount = res.totalCount;
        pagesize = res.pagesize;

      }
    })
  }

  // 求出当前总页码数
  pageCount = Math.ceil(totalCount / pagesize);
  // 下一页
  $('[aria-label="Next"]').on('click', function () {
    currentPage++;
    // 当当前页码大于总页码时,当前页码等于总页码数
    if (currentPage > pageCount) {
      currentPage = pageCount;
      return;
    }
    // 调用数据渲染函数渲染下一页数据
    saveMoney(currentPage);
    // console.log(currentPage);
  })

  // 上一页
  $('[aria-label="Previous"]').on('click', function () {
    currentPage--;
    // 当当前页码小于1时,当前页码等于1
    if (currentPage < 1) {
      currentPage = 1;
      return;
    }
    // 调用数据渲染函数渲染下一页数据
    saveMoney(currentPage);
  })

}